package com.cdplife.ci.core.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * String字符串的工具类
 * @author Shilin.Qu
 *
 */
public class StringUtil {
	
	/**
	 * 将list数据转化为字符串，带有separator
	 * @param list
	 * @param separator
	 * @return
	 */
	public static String listToString(List<?> list, char separator) {    
		StringBuilder sb = new StringBuilder();    
		for (int i = 0; i < list.size(); i++) {        
			sb.append(list.get(i)).append(separator);    
		}    
		return sb.toString().substring(0,sb.toString().length()-1);
	}


	/**
	 * @brief 获得指定日期的后一天
	 * @param date
	 * @return
	 */
	public static String getSpecifiedDayAfter(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day=c.get(Calendar.DATE);
		c.set(Calendar.DATE,day+1);
		String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	/**
	 * @brief 判断时间是否在时间段内
	 * @param nowTime
	 * @param beginTime
	 * @param endTime
	 * @return int
	 */
	public static  int belongCalendar(Date nowTime, Date beginTime, Date endTime) {
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);
		Calendar begin = Calendar.getInstance();
		begin.setTime(beginTime);
		Calendar end = Calendar.getInstance();
		end.setTime(endTime);
		if (date.after(begin) && date.before(end)) {
			return 0;
		}else if(nowTime.compareTo(beginTime)==0 || nowTime.compareTo(endTime) == 0 ){
			return 0;
		}else if(date.before(begin)){
			return -1;
		}else{
			return 1;
		}
	}
}
